import React from 'react';
import { Button, Checkbox, Message } from '@teamix/ui';
import './index.scss';
import exceed from 'utils/apimap';
import { navigate } from 'utils/router';
import utils from 'utils';
export default function index(props) {
  const { AuthFinishedStatus, applicationInfo } = props;
  const doAuth = () => {
    if (AuthFinishedStatus.isBindFinished) {
      return <div className="finished_auth_message">你已完成授权，请勿重复操作</div>;
    }
    return (
      <div>
        <div className="auth_content_do_auth">
          <p>此第三方应用请求获取你在云效Codeup的以下权限：</p>
          <Checkbox disabled defaultChecked="true" className="info_list">
            访问你的个人信息
          </Checkbox>
          <Checkbox disabled defaultChecked="true" className="info_list">
            获取你所有企业下有权限的代码库、组的创建、查看、更新和设置Webhooks的权限
          </Checkbox>
        </div>
        <div style={{ marginTop: '60px' }}>
          <Button
            className="auth_btn"
            type="normal"
            onClick={() =>
              Message.show({
                duration: 2000,
                type: 'warning',
                title: '取消授权',
                style: {
                  position: 'fixed',
                },
                afterClose: () => {
                  navigate(applicationInfo.redirect_uri);
                },
              })
            }
          >
            拒绝
          </Button>
          <Button onClick={doAuthBtn} type="primary">
            同意授权
          </Button>
        </div>
      </div>
    );
  };
  const doAuthBtn = () => {
    const { app_id, grant_type, scopes } = applicationInfo;
    exceed
      .fetch({
        api: 'auth.authorize',
        data: {
          app_id,
          scope: scopes.map(v => v.scope).join(','),
          grant_type,
          state: utils.getUrlSearchParam('state') || '',
        },
      })
      .then((result) => {
        Message.show({
          duration: 2000,
          title: '授权认证成功',
          style: {
            position: 'fixed',
          },
          afterClose: () => {
            navigate(`${result.redirect_uri}`);
          },
        });
      })
      .fail((err) => {
        console.log(err);
      });
  };
  return (
    <div className="auth_content_wrap">
      {doAuth()}
    </div>
  );
}
